import { createRouter, createWebHistory } from "vue-router";
import { store } from './store';

// 对路由统一管理
const routes = [
    // 登录路由
    {
        path: '/',
        name:'Login',
        component: () => import('./Login.vue')
    },
    // 首页路由
    {
        path: '/index',
        component: () => import('./Index.vue')
    },
    // 个人信息路由
    {
        path: '/person/:name',
        component: () => import('./Person.vue')
    },
    // 爱心报表路由
    {
        path: '/points',
        component: () => import('./Points.vue')
    },
    // 认领活动中活动列表
    {
        path: '/activity/:name?',
        component: () => import('./Activities.vue'),
        children: [
            {
                path: 'detail/:id',
                component: () => import('./Detail.vue')
            }
        ]
    },
    // 活动详情路由
    {
        path: '/detail/:id',
        component: () => import('./Detail.vue')
    },
    // 我的报名路由
    {
        path: '/myjoin',
        component: () => import('./MyJoin.vue')
    },
    // 服务记录上传路由
    {
        path: '/server',
        component: () => import('./Server.vue')
    },
    // 我的服务路由
    {
        path: '/myserver',
        component: () => import('./MyServer.vue')
    },
    //我的服务中的服务详情页面
    {
        path: '/serverdetail',
        component: () => import('./ServerDetail.vue')
    },
    // 注册路由
    {
        path: '/register',
        component: () => import('./Register.vue')
    }

]

export var url = 'http://localhost:5000'

export var router = new createRouter({
    history: createWebHistory(),
    routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 获取认证状态
    const isAuthenticated = store.state.isAuthenticated
    // 如果访问的不是登录页或注册页，且未登录
    if (to.path !== '/' && to.path !== '/register' && !isAuthenticated) {
        next('/') // 跳转到登录页
    } else {
        next() // 允许访问
    }
})

